<?php

namespace app\stores\controller\miniapp;

use app\BaseController;
use app\beautiful\model\member\MemberModel;
use app\common\model\MemberConsumeModel;


class Consume extends BaseController{

    public function list(){
        $member=$this->request->member;
        $limit=$this->request->limit;
        $page=$this->request->page;
        $status = $this->request->param('status',1);
        $where=[];
        $where['member_id']=$member['member_id'];
        $where['status']=$status;
        $order['a.id']='desc';
        $res_data=MemberConsumeModel::ulist($where,$order,$page,$limit);
        $all_reward=MemberConsumeModel::where($where)->sum('reward_money');
        $h_s=[];
        $h_s['member_id']=$member['member_id'];
        $h_s['status']=1;
        $have_count=MemberConsumeModel::where($h_s)->count();
        $e_s=[];
        $e_s['member_id']=$member['member_id'];
        $e_s['status']=2;
        $end_count=MemberConsumeModel::where($e_s)->count();
        $res_data['have_count']=$have_count;
        $res_data['end_count']=$end_count;
        $res_data['all_reward']=round($all_reward,2);
        if(!empty($res_data['list'])){
            foreach ($res_data['list'] as &$list){
                if($status!=1){
                    $list['start_time']=date('Y-m-d',strtotime($list['start_time']));
                    $list['end_time']=date('Y-m-d',strtotime($list['end_time']));
                }
            }
        }
        return $this->success($res_data);
    }
    public function made_list(){
        $member=$this->request->member;
        $id = $this->request->param('id',0);
        if($id==0){
            return $this->error('参数错误');
        }
        $one=MemberConsumeModel::where(['id'=>$id,'member_id'=>$member['member_id']])->find();
        if(empty($one)){
            return $this->error('数据不存在');
        }
        $where=[];
        $where['shop_id']=$one['shop_id'];
        $where['type']=$one['type'];
        $where['start_time']=$one['start_time'];
        $order['a.sort']='asc';
        $order['a.member_id']='asc';
        $res_data=MemberConsumeModel::list($where,$order);
        $membermodel=new MemberModel();
        foreach ($res_data as &$v){
            $s=[];
            $s['member_id']=$v['member_id'];
            $one=$membermodel->where($s)->find();
            $v['nick_name']='';
            $v['face']='';
            $v['member_num']='';
            if(!empty($one)){
                $v['nick_name']=$one['nick_name'];
                $v['face']=$one['face'];
                $v['member_num']=$one['member_num'];
            }
        }
        return $this->success($res_data);
    }
}

